<?php

class MSession extends MSingleton {

    const SESSION_KEY    = 'mandrill';
    const SALT_SIZE      = '7';

    const OPENID_AUTH    = 'OPENID_AUTH';
    const NORMAL_AUTH    = 'NORMAL_AUTH';

    const REGISTER_SUCCESS    = 'REGISTER_SUCCESS';
    const REGISTER_USER_EXIST = 'REGISTER_USER_EXIST';


    public function  __construct() {
        parent::__construct();
    }

    public function start() {
        if( !isset ($_SESSION[self::SESSION_KEY]) ) {
            session_start();
        }
    }

    public function setValue($name, $value) {
        $_SESSION[self::SESSION_KEY][$name] = $value;
    }

    public function getValue($name) {
        if ( isset($_SESSION[self::SESSION_KEY][$name]) ) {
            return $_SESSION[self::SESSION_KEY][$name];
        } else {
            return false;
        }
    }

    public function isLogged() {
        if( $this->getValue('userID') != NULL) {
            return True;
        } else {
            return False;
        }
    }


    public function setUserID($userID) {
        $this->setValue('userID', $userID);
    }

    public function getUserID() {
        return $this->getValue('userID');
    }

    public function getUsername() {
        return $this->getValue('username');
    }

    public function setUsername($username) {
        $this->setValue('username', $username);
    }


    public function login($authType, $username, $password) {

        // Login with OpenID or normal authentification ?
        switch($authType) {

            // Normal Authentification
            case self::NORMAL_AUTH :

                // User exists ?
                $user = User::find(array('conditions' => array( 'login' => $username)));
                if($user)
                {
                    // Make the hash and compare it
                    $hash = sha1($user->salt . $username . $password);
                    if( $user->password === $hash ) {
                        $this->setValue('username', $user->login);
                        $this->setValue('userID', $user->id);
                        $this->setValue('userMail', $user->email);
                        return true;
                    }
                }
                return false;

            // OpenID authentification
            case self::OPENID_AUTH :

        }
    }


    public function logout() {
        unset($_SESSION[self::SESSION_KEY]);
        return true;
    }


    public function register($username, $password, $email) {

        // Find an existing user
        $user = User::find(array('conditions' => array('login' => $username)));
        if(!$user) {

            // Make the hash
            $salt = $this->generateSalt();
            $hash = sha1($salt . $username . $password);

            // Insert the User
            $user = new User();
            $user->login = $username;
            $user->password = $hash;
            $user->salt = $salt;
            $user->display_name = $username;
            $user->email = $email;
            $user->creation_date = date_create();
            $user->status = '1';

            if( $user->save()) {
                $this->setValue('username', $user->login);
                $this->setValue('userID', $user->id);
                $this->setValue('userMail', $user->email);
            }
            return self::REGISTER_SUCCESS;
        } else {
            return self::REGISTER_USER_EXIST;
        }

    }


    public function generateSalt() {
        $salt = '';
        for($i=0; $i<self::SALT_SIZE; $i++)
                $salt .= chr(rand(0, 25) + ord('a'));
        return $salt;
    }

}

?>
